package com.singlemountaintech.haogongjiang.qfd.ui.demo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.framework.dialog.AlertDialog;
import com.framework.http.DefaultTransformer;
import com.singlemountaintech.haogongjiang.event.RxBus;
import com.singlemountaintech.haogongjiang.event.RxBus_LoginSuccess;
import com.singlemountaintech.haogongjiang.http.JsonApiWrapper;
import com.singlemountaintech.haogongjiang.http.ProgressSubscriber;
import com.singlemountaintech.haogongjiang.model.response.LoginModel;
import com.singlemountaintech.haogongjiang.qfd.BaseActivityPresenter;
import com.singlemountaintech.haogongjiang.qfd.constant.Keys;
import com.singlemountaintech.haogongjiang.utils.LogUtils;
import com.singlemountaintech.haogongjiang.utils.LoginUtil;
import com.singlemountaintech.haogongjiang.utils.UiUtils;

/**
 * Created by LUOLI on 2018/2/12.
 */

public class LoginActivity extends BaseActivityPresenter<LoginDelegate> {
    @Override
    protected Class<LoginDelegate> getDelegateClass() {
        return LoginDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();

    }

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra(Keys.VALUE);
        if (!TextUtils.isEmpty(stringExtra) && stringExtra.equals("refresh")) {
            LogUtils.eNormal("我曹  ", "我他妈去登录了");
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showLoginDialog();
                }
            }, 500);
        }
    }


    /**
     * 刷新token的接口失败后直接去登录
     */
    public void showLoginDialog() {
        new AlertDialog(this).builder()
                .setTitle("提示")
                .setMsg("登陆过期，请重新登录")
                .setCancelable(false)
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JsonApiWrapper.getApiService().login("13121211891", "123456", false)
                                .compose(new DefaultTransformer<LoginModel>())
                                .subscribe(new ProgressSubscriber<LoginModel>(LoginActivity.this, true, "正在登录...") {
                                    @Override
                                    public void onNext(LoginModel response) {
                                        if (Keys.HTTP_20000 == response.status) {
                                            LoginUtil.putToken(response.token);
                                            LoginUtil.putRefreshToken(response.refreshToken);
                                            UiUtils.showToast(null, "登录成功！");
                                            RxBus.getDefault().post(new RxBus_LoginSuccess());
                                            finish();
                                        } else if (!TextUtils.isEmpty(response.message)) {
                                            if (TextUtils.equals(response.message, "没有找到")) {
                                                UiUtils.showToast(null, "当前账号没有注册，请去注册！");
                                            } else {
                                                UiUtils.showToast(null, response.message);
                                            }

                                        }
                                    }
                                });
                    }
                }).show();
    }
}
